package cn.aitstone.basic.controller;

import cn.aitstone.basic.dto.SmsCodeDto;
import cn.aitstone.basic.dto.EmailCodeDto;
import cn.aitstone.basic.exception.BusinessException;
import cn.aitstone.basic.service.IVerifyCodeService;
import cn.aitstone.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
    * @Title: graph
    * @Description: 获取图形验证码
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/15 12:04
    * @Parameters: [imageCodeKey]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult graph(@PathVariable("imageCodeKey") String imageCodeKey){

        try {
            System.out.println("99999999999999999");
            String imgeBase64Str = verifyCodeService.graph(imageCodeKey);
            return AjaxResult.me().setResultObj(imgeBase64Str);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
    * @Title: smsCode
    * @Description: 短信验证码
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/15 12:03
    * @Parameters: [smsCodeDto]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping ("/smsCode")
    public AjaxResult smsCode(@RequestBody SmsCodeDto smsCodeDto){

        try {
            verifyCodeService.smsCode(smsCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
    * @Title: mailCode
    * @Description: 生成邮箱验证码
    * @Author: Sheldon
    * @Version: 1.0
    * @Date:  2022/7/15 12:03
    * @Parameters: [emailCodeDto]
    * @Return cn.aitstone.basic.utils.AjaxResult
    */
    @PostMapping ("/mailCode")
    public AjaxResult mailCode(@RequestBody EmailCodeDto emailCodeDto){

        try {
            verifyCodeService.mailCode(emailCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }
}
